package myProblem;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;




public class Map extends ExtendableMap {

	public static final int dim = 9;
	



	public static void initMap(ExtendableMap map, List<Integer> obstacles) {
		
		
		map.clear();
		
		int x = 0;
		int y = 0; 
		Set<Integer> nodes = new HashSet<Integer>();
		
		//rij
		for(int j = 0; j< dim; j++)
		{
			//kolom
			for(int i = 1; i< dim; i++)
			{
				int node = i+j*dim;
				if(!obstacles.contains(node)&& !obstacles.contains(node+1))
				{
					map.addBidirectionalLink(""+node, ""+(node+1), 1.0);
				}
				nodes.add(node);
				nodes.add(node+1);
				
			
				if (node % dim != 0 && node <= (dim*(dim-1))) 
				{
					if(!obstacles.contains(node)&& !obstacles.contains(node+dim+1))
					{
						map.addBidirectionalLink(""+node, ""+(node+dim+1), Math.sqrt(2));
					}
				}
			
			}
			
		
			
		}
		
		
		List<Integer> list = new ArrayList<Integer>(nodes);
		Collections.sort(list);
		int counter = 0;
		for (int node : list)
		{
			if (node % dim != 1 && node <= (dim * (dim-1))) 
			{
				if(!obstacles.contains(node)&&!obstacles.contains(node+dim-1))
				{
					map.addBidirectionalLink(""+node, ""+(node+dim-1), Math.sqrt(2));	
					
				}
			}
			
			
			if(!obstacles.contains(node)&& !obstacles.contains(node-dim))
			{
				if (node>dim )
				{
					map.addBidirectionalLink(""+node, ""+(node-dim), 1.0);
				}
			}
			
			
			map.setDistAndDirToRefLocation(""+node, x, y);
			x = x + 50;
			counter ++;
			if(counter == dim)
			{
				x= 0;
				y = y+50;
				counter = 0;
			}
			
		}
		
	}

	
}

